package com.yhcui.monitor.monitor;

import com.yhcui.monitor.bean.AppInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.net.InetSocketAddress;
import java.net.Socket;

@Slf4j
@Component
public class ConnectMonitor implements Monitor {

    @Override
    public boolean mointor(AppInfo appInfo) {
        long start = System.currentTimeMillis();
        try {
            Socket socket = new Socket();
            socket.connect(new InetSocketAddress(appInfo.getDomain(), appInfo.getPort()) , 2000);
            socket.close();
            return true;
        } catch (Exception e) {
            log.error("ConnectMonitor error {}:{}", appInfo.getDomain(), appInfo.getPort(), e);
            return false;
        } finally {
            log.info("ConnectMonitor server:{}, millis total:{}", appInfo.getAppName(), (System.currentTimeMillis() - start));
        }
    }
}
